﻿using System.Xml.Linq;

namespace AzukiMap.MapSource
{
    public class PostGisSource : VectorSource
    {
        /*
        f_table_catalog   | character varying(256) | not null
        f_table_schema    | character varying(256) | not null
        f_table_name      | character varying(256) | not null
        f_geometry_column | character varying(256) | not null
        coord_dimension   | integer                | not null
        srid              | integer                | not null
        type              | character varying(30)  | not null
        */

        public PostGisSource(string name, VectorSourceTypes type, int srid)
            : base(name, type, srid)
        {
            Init();
        }

        public PostGisSource(VectorSourceTypes type)
            : base(type)
        {
            Init();
        }

        /// <summary>
        /// Constructor to create a object from the XML parser.
        /// </summary>
        public PostGisSource()
            : base(VectorSourceTypes.Point)
        {
            Init();
        }

        public string DbName { get; set; }

        public string GeometryColumn { get; set; }

        public string TableName { get; set; }

        public string TableCatalog { get; set; }

        public string TableSchema { get; set; }

        public string GetColumnDataUri { get; set; }

        public string Where { get; set; }

        public void Init()
        {
            DbName = "";
            TableName = "";
            GeometryColumn = "";
            TableCatalog = "";
            TableSchema = "";
            GetColumnDataUri = "";
            Where = "";
        }

        public bool Compare(PostGisSource other)
        {
            if (Type == other.Type &&
                DbName == other.DbName &&
                GeometryColumn == other.GeometryColumn &&
                _srid == other._srid &&
                TableName == other.TableName &&
                TableCatalog == other.TableCatalog &&
                Name == other.Name &&
                TableSchema == other.TableSchema)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override XElement ToXml()
        {
            return ToXml("PostGisSource");
        }

        public override XElement ToXml(string elementName)
        {
            XElement pgSourceXml = base.ToXml(elementName);
            pgSourceXml.Add(
                _xmlCommon.CreateAttr("DbName", DbName),
                _xmlCommon.CreateAttr("GeometryColumn", GeometryColumn),
                _xmlCommon.CreateAttr("TableName", TableName),
                _xmlCommon.CreateAttr("TableCatalog", TableCatalog),
                _xmlCommon.CreateAttr("TableSchema", TableSchema),
                _xmlCommon.CreateAttr("GetColumnDataUri", GetColumnDataUri),
                _xmlCommon.CreateAttr("Where", Where)
                ) ;

            return pgSourceXml;
        }

        public override void Parse(XElement pgSourceXml)
        {
            base.Parse(pgSourceXml);
            DbName = _xmlCommon.GetStringAttr(pgSourceXml, "DbName");
            GeometryColumn = _xmlCommon.GetStringAttr(pgSourceXml, "GeometryColumn");
            TableName = _xmlCommon.GetStringAttr(pgSourceXml, "TableName");
            TableCatalog = _xmlCommon.GetStringAttr(pgSourceXml, "TableCatalog");
            TableSchema = _xmlCommon.GetStringAttr(pgSourceXml, "TableSchema");
            GetColumnDataUri = _xmlCommon.GetStringAttr(pgSourceXml, "GetColumnDataUri");
            Where = _xmlCommon.GetStringAttr(pgSourceXml, "Where");
        }
    }
}